package game;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.geom.Point2D;

class Level_01 extends BaseLevel
{
	Font fontNivel = new Font("Consolas", Font.PLAIN, 25);
	Font fontGameOver = new Font("Consolas", Font.PLAIN, 80);
	Color colorFont = new Color(255, 255, 255, 255);
	Color colorFundo = new Color(0, 0, 0, 150);
	
	Point posNivel = new Point(360, 310);
	Point posGameOver = new Point(200, 330);
	
	Background background;
	Background bg;
	Player player;
	HudPlayer hudPlayer;
	HudTiro hudTiro;
	int nivel;
	float timer;
	float timeNivel = 4f;
	float timeGameOver = 6f;
	boolean increased = false;

	public Level_01()
	{
		
		
		
		bg = new Background(0, new Point2D.Double(0, 0), new Point2D.Double(800, 600));
		player = new Player(2, new Point2D.Double(0, 0), new Point2D.Double(50, 32),
							   new Point2D.Double(0, 0), new Point2D.Double(50, 32));
		hudPlayer = new HudPlayer(6, new Point2D.Double(10, 10),new Point2D.Double(10, 10));
		hudTiro = new HudTiro(7, new Point2D.Double(10, 10),new Point2D.Double(10, 10));	
		
		nivel = 1;
		EnemyManager.getInstance().init(nivel);
		ShootManager.getInstance().init();
		Audios.getInstance().Loop(2);
	}

	public void update()
	{
		if (player.HP <= 0)
		{
			EnemyManager.getInstance().update(player, true);
			hudPlayer.update(player);
			hudTiro.update(player);
			timer += Game.loopTime;
			if (timer > this.timeGameOver)
			{
				timer = 0;
				SceneManager.changeScene(0);
			}
		}
		else
		{
			player.update();
			EnemyManager.getInstance().update(player, false);
			hudPlayer.update(player);
			hudTiro.update(player);
		}
		

		if (EnemyManager.getInstance().totalEnemies == EnemyManager.getInstance().enemiesDead)
		{
			if (increased == false)
			{
				increased = true;
				nivel += 1;
				player.setHP(player.getHPMax());
			}
			
			timer += Game.loopTime;
			if (timer > this.timeNivel)
			{
				timer = 0;
				EnemyManager.getInstance().init(nivel);
			}
		}
		else
		{
			increased = false;
		}
	}

	
	public void draw(Graphics g)
	{
		bg.draw(g);
		
		if(player.HP > 0)
			player.draw(g);
		
		EnemyManager.getInstance().draw(g);
		
		if(player.HP > 0)
			hudTiro.draw(g, player);
		
		hudPlayer.draw(g);
		
		
		if (EnemyManager.getInstance().totalEnemies == EnemyManager.getInstance().enemiesDead)
		{
			this.DesenharLevel(g, "Nivel " + nivel, this.posNivel, this.timeNivel, this.fontNivel);
		}
		
		if(player.HP <= 0)
		{
			this.DesenharLevel(g, "Game Over", this.posGameOver, this.timeGameOver, this.fontGameOver);
			
		}
		
		
	}

	
	public void DesenharLevel(Graphics g, String str, Point pos, float time, Font font)
	{
		
			
		float tempo = this.timer/(time/2); 
		
		if(tempo > 1)
			tempo = 1f;
			
		Color corFundo = new Color(	this.colorFundo.getRed(), 
									this.colorFundo.getGreen(),
									this.colorFundo.getBlue(),
									(int)(this.colorFundo.getAlpha() * tempo));
		
		Color corFonte = new Color(	this.colorFont.getRed(), 
									this.colorFont.getGreen(),
									this.colorFont.getBlue(),
									(int)(this.colorFont.getAlpha() * tempo));
		
		g.setColor(corFundo);
		g.fillRect(0, 0, Game.screenWidth, Game.screenHeigth);
		g.setColor(corFonte);
		g.setFont(font);
		g.drawString(str, (int)pos.getX(), (int)pos.getY());
	}



}

